<?php
html::printPath(
		array(
				'registrationWizard.form-title' => 'user/registrationWizard/step',
				'registrationWizard.program-header' => '',
				'registrationWizard.addProgram' => ''
		),
		'registrationWizard.addProgram-desc'
);
$msg->printMessages();

echo form::open('/user/registrationWizard/addProgram');
if ($batchTypeList) {
	echo '<h3>' . text::getEscaped('seminarBatch-batch_type_code') . '</h3>';
	echo '<ul class="radioButtonList" style="min-height: 400px;">';
    foreach ($batchTypeList as $batchType) {
        if (count($batchType['batchList']) == 0) {
            continue;
        }
        echo '<li id="batchType_' . $batchType['code'] . '" class="batchTypeListItem">';
        echo '<label>';
        echo form::radio('batch_type_code', $batchType['code']) . ' ';
        echo html::escape($batchType['name']);
        echo '</label>';
        
        echo '<div class="batchList">';
        echo '<h3>' . text::getEscaped('registration-batch_code') . '</h3>';
        
        echo '<ul>';
        foreach ($batchType['batchList'] as $batch) {
            echo '<li id="batch_' . $batch['batch_code'] . '" class="batchListItem">';
            echo '<label>' . form::radio('batch_code', $batch['batch_code']) . ' ' . $batch['name'] . ' <span class="description" style="margin-left: 8px;">' . format::dateInterval($batch['begin_date'], $batch['end_date']) . '</span></label>';
            
            echo '<div class="programTypeList">';

            echo '<h3>' . text::getEscaped('registrationWizard.programType') . '</h3>';
            echo '<ul class="programTypeList">';
            foreach ($batch['programTypeList'] as $programType) {
                echo '<li class="programTypeListItem">' .
                    '<label>' .
                    form::radio('program_type_code', $programType['code']) .
                    $programType['name'] .
                    '</label>';
                echo '<div class="accommodationTypeList">';
                
                echo '<h3>' . text::getEscaped('registrationWizard.accomodationType') . '</h3>';
                echo '<ul class="accomodationTypeList">';
                foreach ($programType['accommodationTypeList'] AS $accommodationType) {
                	echo '<li class="accommodationTypeListItem">' .
                			'<label>' .
                			form::radio('accomodation_type_code', $accommodationType['code']) .
                			$accommodationType['name'] . ' ' .
                			format::price($accommodationType['price'], $this->data->priceGroup['currency_code']) .
                			'</label>' .
                			'</li>';
                }
                echo '</ul>';
                
                echo '</div>';
                echo '</li>';
            }
            echo '</ul>';
            
            echo '</div>';
            echo '</li>';
        }
        echo '</ul>';
        echo '</div>';
        echo '</li>';
    }
    echo '</ul>';
}

echo '<p class="okCancelButtonBlock">';
echo form::button('button', text::get('button.cancel'), 'type="button" onclick="closeDialog()" class="ui-priority-secondary"');
echo form::button('submit', text::get('registrationWizard.addProgramButton'), 'onclick="return validate()" class="ui-priority-primary"');
echo '</p>';
echo form::close();
?>

<script type="text/javascript">
    // $('button').button();

    jQuery('.batchTypeListItem > label > input').change(function(event) {
        jQuery('.programTypeList').hide();
        jQuery('.batchListItem > label > input').each(function() {
            this.checked = false;
        });
        jQuery('.batchList').slideUp();
        jQuery(this).closest('.batchTypeListItem').find('.batchList').slideDown();
    });
    
    jQuery('.batchListItem > label > input').change(function(event) {
    	jQuery('.accommodationTypeList').hide();
        jQuery('.programTypeListItem > label > input').each(function() {
            this.checked = false;
        });
        jQuery('.programTypeList').slideUp();
        jQuery(this).closest('.batchListItem').find('.programTypeList').slideDown();
    });

    jQuery('.programTypeListItem > label > input').change(function(event) {
        jQuery('.accommodationTypeListItem > label > input').each(function() {
            this.checked = false;
        });
        jQuery('.accommodationTypeList').slideUp();
        jQuery(this).closest('.programTypeListItem').find('.accommodationTypeList').slideDown();
    });

    function closeDialog() {
        $('#dialogDiv').dialog('close');
    }

    function validate() {
        var selectedBatchType = $('.batchTypeListItem > label > input:checked');
        if (selectedBatchType.size() == 0) {
            var msg = '<?php echo javascript::escape(text::get('registrationWizard.chooseBatchType')); ?>';
            alert(msg);
            return false;
        }

        if (selectedBatchType.get(0).value != 'shortProgram') {
            if ($('.batchListItem > label > input:checked').size() == 0) {
                alert('<?php echo javascript::escape(text::get('registrationWizard.chooseBatch')); ?>');
                return false;
            }

            if ($('.programTypeList input:checked').size() == 0) {
                alert('<?php echo javascript::escape(text::get('registrationWizard.chooseProgramType')); ?>');
                return false;
            }

            if ($('.accomodationTypeList input:checked').size() == 0) {
                alert('<?php echo javascript::escape(text::get('registrationWizard.chooseAccomodationType')); ?>');
                return false;
            }
        }
        else {
            if (document.getElementById('shortProgramDate').value == '') {
                alert('<?php echo javascript::escape(text::get('registrationWizard.chooseDate')); ?>');
                return false;
            }
            if ($('.batchList input:checked').size() == 0) {
                alert('<?php echo javascript::escape(text::get('registrationWizard.chooseShortProgramType')); ?>');
                return false;
            }
        }
        return true;
    }
</script>
